<?php
session_start();

include_once('config.php');
include_once('saetv2.ex.class.php');

$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );

if (isset($_REQUEST['code'])) {
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken( 'code', $keys ) ;
	} catch (OAuthException $e) {
		
		Header("HTTP/1.1 303 See Other"); 
		Header("Location: $code_url"); 
		exit; 
	}
}

if ($token) {
	$_SESSION['token'] = $token;
	setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
}
else
{
	Header("HTTP/1.1 303 See Other"); 
	Header("Location: $code_url"); 
	exit; 
}

$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token']);

$uid_get      = $c->get_uid();
$uid          = $uid_get['uid'];
$user_message = $c->show_user_by_id($uid); //根据ID获取用户等基本信息



if ($page_count == null || $page_count == 0) {
    $page_count = 50;
}

// $ms = $c->home_timeline(); // done


if ($style_string != null) {
    //	echo($style_string);
    $item_array  = array();
    $color_array = array();
    $item_array  = explode(";", $style_string);
    foreach ($item_array as $row) {
        $pair                  = explode(":", $row);
        $color_array[$pair[0]] = $pair[1];
    }
    
}

if ($hide_follow != null) {
    $hide_messages = array();
    $hide_array    = array();
    $hide_array    = explode(";", $hide_follow);
    foreach ($hide_array as $uid) {
        if ($uid != null && $uid != "")
            $hide_messages = array_merge($hide_messages, $c->user_timeline(1, 3, $uid));
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simplified Weibo</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script> 
<script src=" http://tjs.sjs.sinajs.cn/open/api/js/wb.js?appkey=<?php echo WB_AKEY ?>" type="text/javascript" charset="utf-8">	
</script>
<script type="text/javascript"> 

var uid = <?php echo $uid ?>;
var acctoken = "<?php echo $_SESSION['token']['access_token'] ?>";
var akey = "<?php echo WB_AKEY ?>";
var new_status = 0;
var last_id = 0;
var last_bi_id = 0;
var poll_interval = 20000;
var unread_count = 0;
var default_bgcolor = "#E8E8FF";

var div_bi = document.getElementById("bi_timeline");
var div_home = document.getElementById("home_timeline");
var url_bi = "/statuses/bilateral_timeline.json";
var url_home = "/statuses/home_timeline.json";

WB2.login(function(){
});

//alert(WB2.checkLogin());

// get uid
WB2.anyWhere(function(W){
  
W.parseCMD("/account/get_uid.json", function(sResult, bStatus){
	//alert(sResult.uid);
},{
	access_token: acctoken
},{
    method: 'get'
});
});

// get new msg

/*function PollNewMsg() {

	WB2.anyWhere(function(W){
	    
	W.parseCMD("/remind/unread_count.json", function(sResult, bStatus){
		alert(sResult.status);
	    }
		,{
		userid: uid
	},{
	    method: 'get'
	},{
		source: 
	});
	});

}

PollNewMsg();
*/

function addmsg(type, msg){
    /* Simple helper to add a div.
    type is the name of a CSS class (old/new/error).
    msg is the contents of the div */
	unread_count += msg.status;
    $("#msg_notification").attr("innerText",  unread_count + " new message(s)");
	
	if(unread_count == 0)
	{
		$("#msg_notification").hide("fast");
	}
	else
	{
		$("#msg_notification").show("fast");
	}
	
    
}

function insertAfter(referenceNode, newNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function showNewMsg()
{
	if($("#togglegp").attr("value") == "show all")
	{
		$("#togglegp").click();
	}
	
	$("div.new_weibo").show("fast");
	$("div.new_weibo").removeClass("new_weibo").addClass("text");
	$("#msg_notification").hide("fast");
	unread_count = 0;
}

function updateBi()
{
		loadNewMsg(url_bi, last_bi_id, div_bi);
	
        setTimeout(
            'updateBi()', /* Request next message */
            60000 /* ..after 1 seconds */
        );
}

function loadMsg(url, new_count, div)
{

	div.innerHTML = "";
	
	WB2.anyWhere(function(W){
	    
	W.parseCMD(url, function(sResult, bStatus){
			
			
		jQuery.each(sResult.statuses, function(){
			
			var msg_div = document.createElement("div");
			
			if(div == div_home)
			{
				last_id = Math.max(last_id, this.id);
			}
			else
			{
				last_bi_id = Math.max(last_bi_id, this.id);
			}

			
			msg_div.setAttribute("class", "text");
			//msg_div.style.display = "none";
			msg_div.style.backgroundColor = default_bgcolor;
			msg_div.innerHTML =  "<a href='http://weibo.com/" + this.user.id + "/profile'  target='_blank'>" + this.user.name + "</a>: " + this.text;
			if(this.bmiddle_pic != null)
			{
			var pic_span = document.createElement("span");
			pic_span.setAttribute("class", "pichead");
			pic_span.setAttribute("lang", "zh-CN");
			pic_span.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;");
			pic_span.innerHTML = " pic";
			msg_div.appendChild(pic_span);
			
			var pic_div = document.createElement("div");
			pic_div.setAttribute("class", "pic");
			msg_div.appendChild(pic_div);
			
			var pic_img = document.createElement("img");
			pic_img.setAttribute("id", this.id);
			pic_img.setAttribute("src", this.bmiddle_pic);
			pic_div.appendChild(pic_img);

			pic_div.style.display = "none";
		}
		if(this.retweeted_status != null && this.retweeted_status.user != null)
		{
			var rt_span = document.createElement("span");
			rt_span.setAttribute("class", "rthead");
			rt_span.setAttribute("lang", "zh-CN");
			rt_span.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;");
			rt_span.innerHTML = " rt";
			msg_div.appendChild(rt_span);

			
			var rt_div = document.createElement("div");
			rt_div.setAttribute("class", "retweet_text");
			rt_div.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;padding:10px;margin:5px 5px 5px 20px;border:1px solid #ccc;background-color:");
			rt_div.innerHTML =  "<a href='http://weibo.com/" + this.retweeted_status.user.id + "/profile'  target='_blank'>" + this.retweeted_status.user.name + "</a>: " + this.retweeted_status.text;
			msg_div.appendChild(rt_div);
			
			//rt_div.style.display = "none";
			
			if(this.retweeted_status.bmiddle_pic != null)
			{
			var rtpic_span = document.createElement("span");
			rtpic_span.setAttribute("class", "pichead");
			rtpic_span.setAttribute("lang", "zh-CN");
			rtpic_span.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;");
			rtpic_span.innerHTML = " pic";
			rt_div.appendChild(rtpic_span);
			
			var rtpic_div = document.createElement("div");
			rtpic_div.setAttribute("class", "pic");
			rt_div.appendChild(rtpic_div);
			
			rtpic_div.style.display = "none";


			
			var rtpic_img = document.createElement("img");
			rtpic_img.setAttribute("id", this.retweeted_status.id);
			rtpic_img.setAttribute("src", this.retweeted_status.bmiddle_pic);
			rtpic_div.appendChild(rtpic_img);
		}
		
		}
		
		div.appendChild(msg_div);
		
		}
		

	);

    $("div.pic").unbind("click").click(function(){
    $(this).toggle("fast")
    });
  
 
    $("span.rthead").unbind("click").click(function(){
    $(this).next().toggle("fast")
    });
  
    $("span.pichead").unbind("click").click(function(){
    $(this).next().toggle("fast")
    });

	    }
		,
		{
	    count: new_count
	    },
		{
	    method: 'get'
	},{
		access_token: acctoken
		})
		;
	});
}

function loadNewMsg(url, last_id, target_div)
{ 
//	var target_div = document.getElementById("home_timeline");
	WB2.anyWhere(function(W){
	    
	W.parseCMD(url, function(sResult, bStatus){
			
		jQuery.each(sResult.statuses, function(){
			
			if(target_div == div_home)
			{
				last_id = Math.max(last_id, this.id);
			}
			else
			{
				last_bi_id = Math.max(last_bi_id, this.id);
			}
		
			var msg_div = document.createElement("div");
			msg_div.setAttribute("class", "new_weibo");
			msg_div.style.display = "none";
			msg_div.style.backgroundColor = default_bgcolor;
			msg_div.innerHTML =  "<a href='http://weibo.com/" + this.user.id + "/profile'  target='_blank'>" + this.user.name + "</a>: " + this.text;
			if(this.bmiddle_pic != null)
			{
			var pic_span = document.createElement("span");
			pic_span.setAttribute("class", "pichead");
			pic_span.setAttribute("lang", "zh-CN");
			pic_span.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;");
			pic_span.innerHTML = " pic";
			msg_div.appendChild(pic_span);
			
			var pic_div = document.createElement("div");
			pic_div.setAttribute("class", "pic");
			msg_div.appendChild(pic_div);
			
			var pic_img = document.createElement("img");
			pic_img.setAttribute("id", this.id);
			pic_img.setAttribute("src", this.bmiddle_pic);
			pic_div.appendChild(pic_img);

			pic_div.style.display = "none";
		}
		if(this.retweeted_status != null)
		{
			var rt_span = document.createElement("span");
			rt_span.setAttribute("class", "rthead");
			rt_span.setAttribute("lang", "zh-CN");
			rt_span.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;");
			rt_span.innerHTML = " rt";
			msg_div.appendChild(rt_span);

			
			var rt_div = document.createElement("div");
			rt_div.setAttribute("class", "retweet_text");
			rt_div.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;padding:10px;margin:5px 5px 5px 20px;border:1px solid #ccc;background-color:");
			rt_div.innerHTML =  "<a href='http://weibo.com/" + this.retweeted_status.user.id + "/profile'  target='_blank'>" + this.retweeted_status.user.name + "</a>: " + this.retweeted_status.text;
			msg_div.appendChild(rt_div);
			
			//rt_div.style.display = "none";
			
			if(this.retweeted_status.bmiddle_pic != null)
			{
			var rtpic_span = document.createElement("span");
			rtpic_span.setAttribute("class", "pichead");
			rtpic_span.setAttribute("lang", "zh-CN");
			rtpic_span.setAttribute("style", "color:#707070;font-size:12px;font-family:Arial, Helvetica, sans-serif;");
			rtpic_span.innerHTML = " pic";
			rt_div.appendChild(rtpic_span);
			
			var rtpic_div = document.createElement("div");
			rtpic_div.setAttribute("class", "pic");
			rt_div.appendChild(rtpic_div);
			
			rtpic_div.style.display = "none";


			
			var rtpic_img = document.createElement("img");
			rtpic_img.setAttribute("id", this.retweeted_status.id);
			rtpic_img.setAttribute("src", this.retweeted_status.bmiddle_pic);
			rtpic_div.appendChild(rtpic_img);
		}
		
		}
		
		target_div.insertBefore(msg_div, target_div.firstChild);
		
		}

	);

    $("div.pic").unbind("click").click(function(){
    $(this).toggle("fast")
    });
  
 
    $("span.rthead").unbind("click").click(function(){
    $(this).next().toggle("fast")
    });
  
    $("span.pichead").unbind("click").click(function(){
    $(this).next().toggle("fast")
    });

	    }
		,
		{
	    since_id: last_id
	    },
		{
	    method: 'get'
	},{
		access_token: acctoken
		})
		;
	});
}

function waitForMsg(){
    /* This requests the url "msgsrv.php"
    When it complete (or errors)*/
	WB2.anyWhere(function(W){
	    
	W.parseCMD("/remind/unread_count.json", function(sResult, bStatus){
        
		if(sResult.status > 0)
		{
		loadNewMsg(url_home, last_id, div_home);
		addmsg("new", sResult); /* Add response to a .msg div (with the "new" class)*/
	}
        setTimeout(
            function(){waitForMsg()}, /* Request next message */
            poll_interval /* ..after 1 seconds */
        );

	    }
		,{
		userid: uid
	},{
	    method: 'get'
	},{
		source: akey
	});
	});
};

$(document).ready(function(){
	
	div_bi = document.getElementById("bi_timeline");
	div_home = document.getElementById("home_timeline");
	loadMsg(url_home, 100, div_home);
	loadMsg(url_bi, 100, div_bi);
    waitForMsg(); /* Start the inital request */
	updateBi();
	
//	$("div.notification").hide();
});

function hotkey() 
{ 
var a=window.event.keyCode; 
if((a==65)&&(event.shiftKey)) 
{ 
$("input#toggleall").click();
} 
if((a==81)&&(event.shiftKey)) 
{ 
$("input#togglert").click();
} 
} 

document.onkeydown = hotkey; //当onkeydown 事件发生时调用hotkey函数 
 

$(document).ready(function(){

 // $("div.pic").find("img").hide()
  
  
  $("div.pic").click(function(){
  $(this).toggle("fast")
  });
  
 
  $("span.rthead").click(function(){
  $(this).next().toggle("fast")
  });
  
  $("span.pichead").click(function(){
  $(this).next().toggle("fast")
  });
  
  $("input#togglepic").click(function(){
    if($("input#togglepic").attr("value") == "hide pic")
	{
	$("div.pic").hide() 
	$("div.pic").css("display", "none")
	$("input#togglepic").attr("value", "show pic")
	}
	else
	{
	$("div.pic").show() 
	$("div.pic").css("display", "block")
	$("input#togglepic").attr("value", "hide pic")
	}
  }
  );
  
  $("#togglegp").click(function() {
	  
    if($("input#togglegp").attr("value") == "show all")
  	{
  	$("#bi_timeline").hide() 
	$("#home_timeline").show()
  	$("input#togglegp").attr("value", "show bi")
  	}
  	else
  	{
	  	$("#bi_timeline").show() 
		$("#home_timeline").hide()
	  	$("input#togglegp").attr("value", "show all")

  	}  	
	
  });
  
  $("input#togglert").click(function(){
  if($("input#togglert").attr("value") == "hide rt")
  {
	$("div.retweet_text").hide()
	$("div.retweet_text").css("display", "none")
	$("input#togglert").attr("value", "show rt")
	}
	else
	{
		$("div.retweet_text").show()
		$("div.retweet_text").css("display", "block")
	$("input#togglert").attr("value", "hide rt")
	if($("input#togglepic").attr("value") == "hide pic")
	{ 
	}
	else
	{ 
	}
	}
  }
  );
  
    $("input#toggleall").click(function(){
	  if($("input#toggleall").attr("value") == "hide all")
	  {
	$("input#togglert").attr("value", "hide rt")
	$("input#togglepic").attr("value", "hide pic")
	$("input#toggleall").attr("value", "show all")
	}
	else
	{
		$("input#togglert").attr("value", "show rt")
	$("input#togglepic").attr("value", "show pic")
	$("input#toggleall").attr("value", "hide all")
	}
	     $("input#togglepic").click()
	$("input#togglert").click()
	
  });
  
    $("input#togglepic").click()
	//$("input#togglert").click()
});
</script>

<style type="text/css">
div.pic
{
margin:5px 5px 5px 5px;
cursor: pointer;
}

span
{
color:#707070;
font-size:12px;
font-family:Arial, Helvetica, sans-serif;
}

span.rthead
{
cursor: pointer;
width: 20px;
}

span.pichead
{
cursor: pointer;
width: 20px;
}

a
{
text-decoration: none;
}

div.retweet_text
{
line-height: 22px;
}

div.text
{
line-height: 20px;
font-size:14px;
font-family:Arial, Helvetica, sans-serif;
padding:10px;
margin:5px;
border:1px solid #ccc;
}

div.new_weibo
{
line-height: 20px;
font-size:14px;
font-family:Arial, Helvetica, sans-serif;
padding:10px;
margin:5px;
border:1px solid #ccc;
}

div.notification
{
line-height: 20px;
font-size:14px;
font-family:Arial, Helvetica, sans-serif;
padding:10px;
margin:5px;
border:1px solid #FACC2E;
background-color: #F7D358;
}

</style>
</head>

<body>
	<div lang="zh-CN" id= "msg_notification" class="notification" style="font-size:14px;font-family:Arial, Helvetica, sans-serif;padding:10px;margin:5px;border:1px solid #ccc;display:none;" onClick="showNewMsg()">
	</div>

	<div id="toggles" lang="zh-CN" class="text">
		<input id="togglegp" type="button" name="togglegp" value="show bi">
		<input id="togglepic" type="button" name="togglepic" value="hide pic"/>
		<input id="togglert" type="button" name="togglepic" value="hide rt"/>
		<input id="toggleall" type="button" name="toggleall" value="show all"/>
	</div>
	
	<div lang="zh-CN" id="home_timeline" class="timeline" style="font-size:14px;font-family:Arial, Helvetica, sans-serif;padding:10px;margin:5px;border:1px solid #ccc;">
		<div id="headmark1"></div>
	</div>

	<div lang="zh-CN" id="bi_timeline" class="timeline" style="font-size:14px;font-family:Arial, Helvetica, sans-serif;padding:10px;margin:5px;border:1px solid #ccc;display:none;">
	</div>
	
</body>

</html>